//这个页面是一个网络类管理

import axios, { Method } from "axios";
//引入账户数据状态管理类
import AccountStore, { UserInfo } from './AccountStore'//用户信息结构接口
// 枚举 | 定义的所有接口路径
enum RequestPath {
    login = "/users/login",//登录接口
    signup = "/users/signup",//注册接口
    operationalGet = "/operational/get",//运营信息接口

    categoryListGet = '/goods/category/get',//商品分类列表接口
    goodsGet = '/goods/get',//商品详情接口
    goodsListGet = '/goods/list/get',//商品列表接口
    goodsListSearch= '/goods/list/search',//商品列表搜索接口

    shopGet = '/shop/get', //购物车信息接口
    shopAdd = '/shop/add',//添加购物车接口
    shopUpdate = '/shop/update',//更新购物车接口

    orderCreate = '/order/create',//创建订单接口        
    orderListGet = '/order/list/get',//订单列表接口
    orderUpdate = '/order/update',//更新订单接口

    evaluationCreate = '/evaluation/create',//创建评价接口
    evaluationListGet = '/evaluation/list/get'//评价列表接口
    
}

// 创建一个请求worker实例
const worker = axios.create({
    baseURL: 'http://localhost:3000/api/v1',//基础URL
    timeout: 5000,//请求超时时间5s
    headers: {
        // 移除默认的token设置
    } //请求头默认配置
})
// 定义登录注册的接口数据返回结构
export interface LoginResponseData {
    msg: 'error' | 'ok',
    error?: string,
    info: UserInfo,
    token?: string
}

export interface OperationalItemData {
    id: number,
    name: string,
    status: number,
    created_at: string,
    cover: string,
    content: string,
    uri: string
}

export interface OperationalResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: OperationalItemData[]
}

export interface CategoryItemData {
    id: number,
    name: string,
    description: string,
    created_at: string,
    sort: number
}

export interface CategroyResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: CategoryItemData[]
}

export interface GoodsItemData {
    id: number,
    name: string,
    description: string,
    created_at: string,
    detail?: string,
    price: number,
    discounted_price: number,
    stock: number,
    image: string,
    status: number,
    brand: string,
    category: CategoryItemData
}

export interface GoodsResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: GoodsItemData[]
}

// 购物车中的商品信息模型
export interface ShopCarGoodsData {
    count: number,
    item: GoodsItemData
}
// 购物车模型
export interface ShopCarData {
    id: number,
    user_id: number,
    goods?: ShopCarGoodsData[]
}
// 购物车接口返回的数据结构模型
export interface ShopCarResponseData {
    msg: 'error' | 'ok',
    error?: string,
    data?: ShopCarData
}


// 订单模型
export interface OrderData {
    id: number,
    user_id: number,
    goods?: ShopCarGoodsData[],
    address: string,
    price: number,
    save_price:number,
    state: 0 | 1 | 2 | 3,
    created_at: string
}
// 订单接口返回的数据结构模型
export interface OrderResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: OrderData[]
}

// 评价模型
export interface EvaluationItemData {
    id: number,
    user: UserInfo,
    gid: number,
    content: string,
    star: number,
    created_at: string
}

export interface EvaluationResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: EvaluationItemData[]
}

// 定义请求拦截器 - 只在有token时设置header
worker.interceptors.request.use((config) => {
    const account = AccountStore()
    // 只有在用户已登录且有token时才设置token头
    if (account.isLogin && account.token && account.token.length > 0) {
        config.headers.token = account.token
    } else {
        // 未登录时确保不发送token头
        delete config.headers.token
    }
    return config
}, (error) => {
    return Promise.reject(error)
})

// 定义响应拦截器
worker.interceptors.response.use((response) => {
    return response;
}, (error)=>{
    if (error.response && error.response.status == 401) {
        // 直接退出登录
        AccountStore().logout()
        window.location.href = '/#/login'
    }
    return Promise.reject(error);
})

// 发起请求的方法
function startRequest(path: RequestPath | string, method: Method, params: any) {
    let config:any = {
        method: method,
        url: path
    }
    // get方法将参数放在params中
    if (method == 'GET' || method == 'get') {
        config.params = params
    // post方法将参数放在data中
    } else {
        config.data = params
    }
    // 会返回一个pormiss
    return worker.request(config)
}
// 导出
export {
    worker,
    RequestPath,
    startRequest
}